//*****************************************************************
// TransformThread.h
//
//  Created on: Jan 21, 2012
//      Author: Christopher Wood
//              Robert Livosi
//
//  $Id: TransformThread.h 24 2012-01-28 02:54:38Z caw4567@gmail.com $
//*****************************************************************

#ifndef TRANSFORMTHREAD_H_
#define TRANSFORMTHREAD_H_

#include "Project2.h"
#include "IOThreadGroup.h"

// Resolve cyclic dependency
class IOThreadGroup;

/**
 * This is an abstract class that can be used to make any valid
 * transformation function for the plant.
 */
class TransformThread
{
public:
	/**
	 * Create a new transform thread with the specified I/O group.
	 *
	 * @param ioGroup - analog I/O thread object.
	 */
	TransformThread(IOThreadGroup* ioGroup);

	/**
	 * Default destructor.
	 */
	virtual ~TransformThread();

	/**
	 * Retrieve the I/O thread group associated with this transform object.
	 *
	 * @return a pointer to the I/O thread group.
	 */
	IOThreadGroup* getSignalGroup();

	/**
	 * Retrieve the transform execution semaphore used to trigger a conversion.
	 *
	 * @return a pointer to the transform semaphore.
	 */
	sem_t* getTransformSem();

	/**
	 * Set the thread state of this transform thread.
	 *
	 * @param alive - boolean flag indicating the alive state of this thread.
	 */
	void setThreadState(bool alive);

	/**
	 * Determine if this thread is still alive (or should still be alive)
	 *
	 * @return true if still alive, false otherwise.
	 */
	bool isAlive();

	/**
	 * Release this thread from its semaphore.
	 */
	void release();

	/**
	 * Start the thread.
	 */
	virtual void run() = 0;

protected:
	// Our execution semaphore
	sem_t transformSem;

	// The transform thread unique ID
	pthread_t transformThreadID;

	// Pointer to the analog I/O object
	IOThreadGroup* ioGroup;

	// Boolean flag controlling execution
	bool alive;
};

#endif /* TRANSFORMTHREAD_H_ */
